<?php
require_once('client_config.php');
require_once('ntml_soapclient.php');
$client = new MyNTMLSoapClient('./ews/services.wsdl', $soap_options);
	$client->set_profile($user_profile['login'], $user_profile['password']);
if(isset($_GET['Id']) && !isset($_POST['id'])){
	$appointment = $client->get_appointment($_GET['Id'],&$errcode, &$error);
	if($errcode !== "NoError"){
		echo "Error: " . $errcode . "( " . $error ." )";
	}else{
		$form = "
		<form method='post' action='update_appointment.php'>
		<table align='center'>
			<caption><font color='blue'><b>Update an appointment</b></caption>
			<tr><td></td><td colspan='2'><input id='id' type='hidden' name='id' value=' ". urlencode($appointment->get_id()) . "'/></td></tr>
			<tr><td></td><td colspan='2'><input id='changekey' type='hidden' name='changekey' value=' ". $appointment->get_changekey() . "'/></td></tr>
			<tr><td>Subject: </td><td colspan='2'><input id='subject' type='text' name='subject' value=' ". $appointment->get_subject() . "'/></td></tr>
			<tr><td>Description:</td><td colspan='2'><textarea rows='10' cols='60' id='description' name='description'>" . $appointment->get_desc() . " </textarea></td></tr>
			<tr><td>Location: </td><td colspan='2'><input id='location' type='text' name='location' value='" .$appointment->get_location() . "'/></td></tr>
			<tr><td>Categories: </td><td colspan='2'><input id='categories' type='text' name='categories' value='" .implode(',', $appointment->get_categories()) . "'/> (Separate categories by comma)</td></tr>";
		$form .="<tr><td>Start date/time:</td><td> <input id='date' type ='text' name='startdate' value='" . substr($appointment->get_start_datetime(),0,10) . "'/>YYYY-MM-DD</td> <td><input id='time' type ='text' name='starttime' value='" . substr($appointment->get_start_datetime(),11,8) . "'/>HH-Mi-SS</td></tr>";
		$form .="<tr><td>End date/time:</td><td> <input id='date' type ='text' name='enddate' value='" . substr($appointment->get_end_datetime(),0,10) . "'/>YYYY-MM-DD</td> <td><input id='time' type ='text' name='endtime' value='" . substr($appointment->get_end_datetime(),11,8) . "'/>HH-Mi-SS</td></tr>
			<tr><td colspan='3' align='center'><input type='submit' value='Update'></td></tr>
		</table>
		</form>";
		echo $form;
	}
}else{
	if(isset($_POST['id'])){
		$appointment = new Appointment();
		$appointment->set_id(urldecode($_POST['id']));
		$appointment->set_changekey($_POST['changekey']);
		$appointment->set_subject($_POST['subject']);
		$appointment->set_desc($_POST['description']);
		$appointment->set_location($_POST['location']);
		$appointment->set_categories(explode(',',$_POST['categories']));
		$appointment->set_start_datetime($_POST['startdate'] . 'T' . $_POST["starttime"]);
		$appointment->set_end_datetime($_POST['enddate'] . 'T' . $_POST["endtime"]);
		$id = $client->update_appointment($appointment, $errcode, $error);
		if($errcode == "NoError"){
			header('Location: ' . dirname($_SERVER['PHP_SELF']) .'/get_appointment.php?Id=' . urlencode($id));
		}else{
			echo "Error: " . $errcode . "( " . $error ." )";
		}
	}
}
?>